import { Locale } from '@/messages/locale'

export interface MessageFormat {
  parse(args: string[], locale: Locale): string
}

export class ReplaceMessageFormat implements MessageFormat {
  pattern: string

  constructor (pattern: string) {
    this.pattern = pattern
  }

  parse (args: string[], locale: Locale): string {
    let str = this.pattern
    for (let i = 0; i < args.length; i++) {
      const reg = new RegExp('\\{' + i + '\\}', 'g')
      str = str.replace(reg, args[i])
    }
    return str
  }
}
